package com.sg.service.base.component.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.component.req.*;
import com.sg.entity.OmsMessage;
import com.sg.service.base.component.MOmsMessageService;
import com.wicket.okrcomponent.common.dto.FieldInfo;
import com.wicket.okrcomponent.common.exception.BizException;
import com.wicket.okrcomponent.common.tools.StringUtils;
import com.wicket.okrcomponent.dal.mapper.mbg.OmsMessageMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsMessageServiceImpl
        extends ServiceImpl<OmsMessageMapper, OmsMessage>
        implements MOmsMessageService {

    @Autowired
    OmsMessageMapper omsMessageMapper;

    /**
     * 2-5-02新增消息[3980]
     * gen by moon at 8/15/2023, 8:31:56 PM
     */
    @Trace(operationName = "2-5-02新增消息")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addMessage(OmsMessage omsMessage) {
        // TODO: 2024/1/19
        if (!StringUtils.isEmpty(omsMessage.getSenderProfilePictureAddress())) {
            omsMessage.setSenderProfilePictureAddress(CommonFunctionHelper.replaceFilePath(omsMessage.getSenderProfilePictureAddress()));
        }
        omsMessage.setMessageId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsMessage.setIsValid("FALSE");
        if (super.save(omsMessage)) {
            return omsMessage.getMessageId();
        } else {
            return "-1";
        }

    }

    /**
     * 2-5-03修改消息[7522]
     * gen by moon at 10/15/2023, 3:20:12 AM
     */
    @Trace(operationName = "2-5-03修改消息")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateMessage(OmsMessage omsMessage) {

        return super.updateById(omsMessage);
    }

    /**
     * 2-5-02查询消息详情[7115]
     * gen by moon at 11/11/2023, 2:38:10 AM
     */
    @Trace(operationName = "2-5-02查询消息详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsMessage queryMessageDetail(QueryMessageDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsMessage> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsMessage> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsMessage retData = retList.get(0);
                // TODO: 2024/5/30
                retData.setSenderProfilePictureAddress(CommonFunctionHelper.getFilePath(retData.getSenderProfilePictureAddress()));
                retData.setMediaAddress(CommonFunctionHelper.getFilePath(retData.getMediaAddress()));
                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-02批量查询消息[3983]
     * gen by moon at 5/16/2023, 6:36:31 PM
     */
    @Trace(operationName = "2-5-02批量查询消息")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessage> batchQueryMessage(BatchQueryMessageReq reqDto) {

        try {
            QueryWrapper<OmsMessage> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getMessageList() == null || reqDto.getMessageList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("message_id", reqDto.getMessageList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessage.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessage.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessage.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            // TODO: 2024/5/30
            List<OmsMessage> retList = super.list(queryWrapper);
            for (OmsMessage one : retList) {
                one.setSenderProfilePictureAddress(CommonFunctionHelper.getFilePath(one.getSenderProfilePictureAddress()));
                one.setMediaAddress(CommonFunctionHelper.getFilePath(one.getMediaAddress()));
            }
            return retList;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-02删除消息[3984]
     * gen by moon at 9/14/2022, 4:09:15 PM
     */
    @Trace(operationName = "2-5-02删除消息")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteMessage(String messageId) {

        return super.removeById(messageId);
    }

    /**
     * 2-5-02查询消息列表[7066]
     * gen by moon at 10/14/2023, 11:31:21 PM
     */
    @Trace(operationName = "2-5-02查询消息列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessage> queryMessageList(QueryMessageListReq reqDto) {

        try {
            QueryWrapper<OmsMessage> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessage.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessage.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessage.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            // TODO: 2024/5/30
            List<OmsMessage> retList = super.list(queryWrapper);
            for (OmsMessage one : retList) {
                one.setSenderProfilePictureAddress(CommonFunctionHelper.getFilePath(one.getSenderProfilePictureAddress()));
                one.setMediaAddress(CommonFunctionHelper.getFilePath(one.getMediaAddress()));
            }
            return retList;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-02查询推第三方问题列表[7069]
     * gen by moon at 5/31/2023, 11:04:34 PM
     */
    @Trace(operationName = "2-5-02查询推第三方问题列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessage> queryPushThirdPartyMessagesList(QueryPushThirdPartyMessagesListReq reqDto) {

        try {
            QueryWrapper<OmsMessage> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessage.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessage.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessage.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("operate_time");
            return ((List<OmsMessage>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-03批量查询消息By归属内容ID列表[7543]
     * gen by moon at 10/15/2023, 6:08:35 PM
     */
    @Trace(operationName = "2-5-03批量查询消息By归属内容ID列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessage> batchQueryMessageByEntityIdListByIds(BatchQueryMessageByEntityIdListByIdsReq reqDto) {

        try {
            QueryWrapper<OmsMessage> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getMessageList() == null || reqDto.getMessageList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("entity_id", reqDto.getMessageList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessage.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessage.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessage.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsMessage>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-02批量删消息[7621]
     * gen by moon at 10/29/2023, 9:33:13 PM
     */
    @Trace(operationName = "2-5-02批量删消息")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteMessage(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 2-5-02查询待处理消息列表[7751]
     * gen by moon at 11/7/2023, 6:59:29 PM
     */
    @Trace(operationName = "2-5-02查询待处理消息列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessage> queryPendingMessagesList(QueryPendingMessagesListReq reqDto) {

        try {
            QueryWrapper<OmsMessage> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getPendingMessagesList() == null || reqDto.getPendingMessagesList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("entity_id", reqDto.getPendingMessagesList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessage.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessage.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessage.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsMessage>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-02查询通道内消息列表[7887]
     * gen by moon at 11/20/2023, 2:05:01 PM
     */
    @Trace(operationName = "2-5-02查询通道内消息列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessage> queryWithinChannelMsgList(QueryWithinChannelMsgListReq reqDto) {

        try {
            QueryWrapper<OmsMessage> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            // TODO: 2023/11/20 手写代码
            if (reqDto.getMessageImportantLevel() != null) {
                queryWrapper.ge("message_important_level", reqDto.getMessageImportantLevel());
                reqDto.setMessageImportantLevel(null);
            }


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessage.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessage.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessage.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("create_time");
            queryWrapper.orderByDesc("operate_time");
// TODO: 2024/5/30
            List<OmsMessage> retList = super.list(queryWrapper);
            for (OmsMessage one : retList) {
                one.setSenderProfilePictureAddress(CommonFunctionHelper.getFilePath(one.getSenderProfilePictureAddress()));
                one.setMediaAddress(CommonFunctionHelper.getFilePath(one.getMediaAddress()));
            }
            return retList;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-02执行归属内容批量查消息[7989]
     * gen by moon at 11/20/2023, 2:05:04 PM
     */
    @Trace(operationName = "2-5-02执行归属内容批量查消息")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessage> implementBelongContBatchQueryMsg(ImplementBelongContBatchQueryMsgReq reqDto) {

        try {
            QueryWrapper<OmsMessage> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getMessageList() == null || reqDto.getMessageList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("entity_id", reqDto.getMessageList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessage.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessage.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessage.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("create_time");
            queryWrapper.orderByDesc("operate_time");
            List<OmsMessage> retList = super.list(queryWrapper);
            for (OmsMessage one : retList) {
                one.setSenderProfilePictureAddress(CommonFunctionHelper.getFilePath(one.getSenderProfilePictureAddress()));
                one.setMediaAddress(CommonFunctionHelper.getFilePath(one.getMediaAddress()));
            }
            return retList;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-02-批量改消息[8114]
     * gen by moon at 12/9/2023, 9:11:47 PM
     */
    @Trace(operationName = "2-5-02-批量改消息")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateMessage(List<OmsMessage> omsMessage) {

        if (omsMessage == null || omsMessage.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsMessage);

    }

    /**
     * 2-5-02执行主键批量查消息[8128]
     * gen by moon at 1/21/2024, 9:44:50 PM
     */
    @Trace(operationName = "2-5-02执行主键批量查消息")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessage> implementPrimaryKeyBatchQueryMsg(ImplementPrimaryKeyBatchQueryMsgReq reqDto) {

        try {
            QueryWrapper<OmsMessage> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getMessageList() == null || reqDto.getMessageList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("message_id", reqDto.getMessageList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessage.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessage.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessage.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("create_time");
            queryWrapper.orderByDesc("operate_time");
            List<OmsMessage> retList = super.list(queryWrapper);
            for (OmsMessage one : retList) {
                one.setSenderProfilePictureAddress(CommonFunctionHelper.getFilePath(one.getSenderProfilePictureAddress()));//otherModel: ALL_PATH
                one.setMediaAddress(CommonFunctionHelper.getFilePath(one.getMediaAddress()));
            }
            return retList;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-02执行引用消息批量查消息[8200]
     * gen by moon at 12/23/2023, 8:00:05 PM
     */
    @Trace(operationName = "2-5-02执行引用消息批量查消息")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessage> implementQuoteMsgBatchQuerylMessage(ImplementQuoteMsgBatchQuerylMessageReq reqDto) {

        try {
            QueryWrapper<OmsMessage> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getMessageList() == null || reqDto.getMessageList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("reference_message_id", reqDto.getMessageList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessage.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessage.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessage.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsMessage>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

}

